package cn.bjxy.shishaolong.request_answer.controller;


import cn.bjxy.shishaolong.common.ResultMsg;
import cn.bjxy.shishaolong.common.StatusCode;
import cn.bjxy.shishaolong.request_answer.entity.Answer;
import cn.bjxy.shishaolong.request_answer.entity.Request;
import cn.bjxy.shishaolong.system.annotation.Permission;
import cn.bjxy.shishaolong.system.exception.OperatorFailureException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 时少龙
 * @since 2019-03-16
 */
@RestController
@CrossOrigin
@RequestMapping("/api/request_answer/answer")
@Slf4j
public class AnswerController extends RequestAnswerBaseController {

    /**
     * 保存
     *
     * @param answer
     * @return
     */
    @PostMapping("/save")
    @Permission
    public ResultMsg save(HttpServletRequest request, @RequestBody Answer answer) {
        try {
            if (answer == null) {
                return ResultMsg.create(StatusCode.PARAMETER_ERROR, StatusCode.PARAMETER_ERROR_MSG);
            }
            // 给修改日期赋值
            Integer currentUserId = getCurrentUserId(request);
            answer.setUpdateAt(LocalDateTime.now()).setUserId(currentUserId).setPid(-1);

            if (isAdopt(answer.getRequestId())) {
                return retHasAdopt();
            }

            answerService.saveOrUpdate(answer);
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.SAVE_FAILURE, StatusCode.SAVE_FAILURE_MSG);
        }
    }

    /**
     * 对一级回答的追问
     *
     * @param answer
     * @return
     */
    @PostMapping("/attach-request")
    @Permission
    public ResultMsg attachRequest(HttpServletRequest request, @RequestBody Answer answer) {
        try {
            if (answer == null) {
                return ResultMsg.create(StatusCode.PARAMETER_ERROR, StatusCode.PARAMETER_ERROR_MSG);
            }
            // 给修改日期赋值
            Integer userId = getCurrentUserId(request);
            answer.setUpdateAt(LocalDateTime.now()).setUserId(userId);
            // 判断， 问题未采纳，追问者为提问者本人
            if (isAdopt(answer.getRequestId())) {  // 已采纳
                return retHasAdopt();
            }
            Request requestorOwn = isRequestorOwn(userId, answer.getRequestId());
            if (requestorOwn == null)
                return retNotOwnOperator();

            answerService.saveOrUpdate(answer);
            return ResultMsg.success();
        } catch (OperatorFailureException ofe) {
            ofe.printStackTrace();
            return ResultMsg.create(ofe.getCode(), ofe.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.SAVE_FAILURE, StatusCode.SAVE_FAILURE_MSG);
        }
    }

    /**
     * 二级再回答，再次追问
     *
     * @param answer
     * @return
     */
    @PostMapping("/attach-reply")
    @Permission
    public ResultMsg attachReply(HttpServletRequest request, @RequestBody Answer answer) {
        try {
            if (answer == null) {
                return ResultMsg.create(StatusCode.PARAMETER_ERROR, StatusCode.PARAMETER_ERROR_MSG);
            }
            // 给修改日期赋值
            Integer userId = getCurrentUserId(request);
            answer.setUpdateAt(LocalDateTime.now()).setUserId(userId);
            // 判断， 问题未采纳，追问者为提问者本人
            if (isAdopt(answer.getRequestId())) {
                return retHasAdopt();
            }

            /*Request requestorOwn = isRequestorOwn(userId, answer.getRequestId());
            Answer retAaswerOwn = isAnswererOwn(userId, answer.getPid());
            if (requestorOwn != null || retAaswerOwn != null) {
                if (!canReply(answer.getPid(), userId)) { // 不允许再次回复
                    return ResultMsg.create(StatusCode.NOT_ALLOW_SECOND_REPLY,StatusCode.NOT_ALLOW_SECOND_REPLY_MSG);
                }
            } else { // 均为null 说明：既不是提问者，也不是回答者
                return retNotOwnOperator();
            }*/

            answerService.saveOrUpdate(answer);
            return ResultMsg.success();
        } catch (OperatorFailureException ofe) {
            ofe.printStackTrace();
            return ResultMsg.create(ofe.getCode(), ofe.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.SAVE_FAILURE, StatusCode.SAVE_FAILURE_MSG);
        }
    }
}
